package cpoo.oldNotUsed;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

public class ImagePainter {
	//private BufferedImage actualFrame;
	private AffineTransform[] transforms;
	
	private int startXPosition = 0;
	private int startYPosition = 0;
	
	private BufferedImage image;
	private AffineTransform actualTransformation;
	private int nextTransformationIndex = 0;
	public ImagePainter (BufferedImage startImage, AffineTransform[] transforms){
//		actualFrame = startImage;
		this.transforms = transforms;
		
		Point2D upperLeft = new Point(0,0);
		double minX = upperLeft.getX();
		double minY = upperLeft.getY();
		for (AffineTransform transform:transforms){
			transform.transform(upperLeft, upperLeft);
			if (upperLeft.getX() < minX){
				minX = upperLeft.getX();
			}
			if (upperLeft.getY() < minY){
				minY = upperLeft.getY();
			}
		}
		Point2D lowerRight = new Point(startImage.getWidth(),startImage.getHeight());
		double maxX = lowerRight.getX();
		double maxY = lowerRight.getY();
		for (AffineTransform transform:transforms){
			transform.transform(lowerRight, lowerRight);
			if (lowerRight.getX() > maxX){
				maxX = lowerRight.getX();
			}
			if (lowerRight.getY() > maxY){
				maxY = lowerRight.getY();
			}
		}
		
		startXPosition = (int)-minX;
		startYPosition = (int)-minY;
		
		image = new BufferedImage((int)(maxX - minX), (int)(maxY - minY), BufferedImage.TYPE_INT_ARGB);
		Graphics2D graphics = image.createGraphics();
		graphics.setColor(new Color(0xFF000000));
		graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
		if (transforms.length > 0){
			transforms[0].translate((double)startXPosition, (double)startYPosition);
			actualTransformation = transforms[0];
			nextTransformationIndex = 1;
		}
		
		printImage(startImage, startXPosition, startYPosition);
	}
	
	public void drawNextFrame(BufferedImage frame){
		Point point = new Point(0,0);
		actualTransformation.transform(point, point);
		printImage(frame, point.x, point.y);
		actualTransformation.concatenate(transforms[nextTransformationIndex]);
		nextTransformationIndex ++;
	}
	
	private void printImage(BufferedImage added, int x, int y){
		for (int i=0; i<added.getWidth(); i++){
			for (int j=0; j<added.getHeight(); j++){
				int rgb = added.getRGB(i, j);
				
				mixColors(rgb, x+i, y+j);
			}
		}
	}
	
	public BufferedImage getPainting(){
		return image;
	}
	
	private void mixColors(int rgb, int x, int y){
		if (x<0 || y<0 || x>=image.getWidth() || y>=image.getHeight()){
			return;
		}
		int actualRGB = image.getRGB(x, y);
		int alpha = (actualRGB & 0xFFFFFF);
//		System.out.println(alpha);
		if (alpha == 0){
			
			image.setRGB(x, y, rgb);
		} else {
			int mask = 0xFF000000&rgb;
			int r = (actualRGB & 0x00FF0000) >> 16;
			int nr = (rgb & 0x00FF0000) >> 16;
			r = ((r+nr)/2)<<16;
			
			int g = (actualRGB & 0x0000FF00) >> 8;
			int ng = (rgb & 0x0000FF00) >> 8;
			g = ((g+ng)/2)<<8;
			
			int b = (actualRGB & 0x000000FF);
			int nb = (rgb & 0x000000FF);
			b = ((b+nb)/2);
			
			rgb = mask | r | g | b;
			
			image.setRGB(x, y, rgb);
		}
	}
}
